package com.jiuyuan.controller;

import com.jiuyuan.entity.Salary;
import com.jiuyuan.entity.vo.RespBean;
import com.jiuyuan.service.ISalaryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiuyuan
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/salary/sob")
public class SalaryController {
    @Autowired
    ISalaryService salaryService;

    @ApiOperation("获取所有帐套")
    @GetMapping("/")
    public RespBean list(){
        return RespBean.success(salaryService.list());
    }

    @ApiOperation("添加帐套")
    @PostMapping("/")
    public RespBean add(@RequestBody Salary salary){
        salary.setCreateDate(LocalDateTime.now());
        if (salaryService.save(salary)) {
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @ApiOperation("删除帐套")
    @DeleteMapping("/{id}")
    public RespBean add(@PathVariable Integer id){

        if (salaryService.removeById(id)) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }


    @ApiOperation("更新帐套")
    @PutMapping("/")
    public RespBean update(@RequestBody Salary salary){
        if (salaryService.updateById(salary)) {
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }
}
